package com.guaju.yueshangLife.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.adapter.NoneCommonRecyclerViewAdapter;
import com.guaju.yueshangLife.adapter.OrderGoodsDetailAdapter;
import com.guaju.yueshangLife.adapter.OrderListAdapter;
import com.guaju.yueshangLife.base.MyAppcomActivity;
import com.guaju.yueshangLife.bean.CartGoods;
import com.guaju.yueshangLife.bean.CouponsBean;
import com.guaju.yueshangLife.databinding.ActivityOrderGoodsListBinding;
import com.guaju.yueshangLife.recyclerView.divider.DividerItemDecoration;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class OrderGoodsListActivity extends MyAppcomActivity {

    private ActivityOrderGoodsListBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_order_goods_list);
        initView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
//        new NoneCommonRecyclerViewAdapter<>()
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);
        binding.recyclerView.addItemDecoration(dividerItemDecoration);


        Intent intent = getIntent();
        String delivery = intent.getStringExtra("delivery");
        ArrayList<CartGoods> goods = (ArrayList<CartGoods>) intent.getSerializableExtra("goods");
        if (goods!=null&&!goods.isEmpty()){
        for (CartGoods bean : goods) {
            bean.setDelivery(delivery);
        }
        }

        OrderGoodsDetailAdapter orderListAdapter = new OrderGoodsDetailAdapter(OrderGoodsListActivity.this, goods);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(OrderGoodsListActivity.this);
        binding.recyclerView.setLayoutManager(linearLayoutManager);
        binding.recyclerView.setAdapter(orderListAdapter);


        binding.tvGoodsNum.setText(goods.size() + "件商品");

        if (intent.hasExtra("coupons")) {
            List<CouponsBean> counpons = (List<CouponsBean>) intent.getSerializableExtra("coupons");
            if (counpons != null && !counpons.isEmpty()) {
                binding.tvGetCoupon.setVisibility(View.VISIBLE);
            }
        } else {
            binding.tvGetCoupon.setVisibility(View.GONE);
        }


    }

    private void initView() {
        TextView top_tv_title = binding.top.findViewById(R.id.top_tv_title);
        top_tv_title.setText("商品清单");
        binding.top.findViewById(R.id.fl_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        FrameLayout fl_back = binding.top.findViewById(R.id.fl_back);
        fl_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
